package com.angysnail.guava.collection;

import java.util.Map;
import java.util.Set;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Range;
import com.google.common.collect.Sets;

public class CollectionTest {

	public static void main(String[] args) {
		Person p1 = new Person("zcy", 11, 1);
		Person p2 = new Person("zcy1", 21, 0);
		Person p3 = new Person("zcy2", 31, 0);
		Set<Person> persons = Sets.newHashSet(p1, p2, p3);
		ImmutableMap<String, Person> map = FluentIterable.<Person>from(persons).<String>uniqueIndex(p -> {
			return p.name;
		});
		// {zcy2=com.angysnail.guava.collection.CollectionTest$Person@78308db1,
		// zcy=com.angysnail.guava.collection.CollectionTest$Person@7440e464,
		// zcy1=com.angysnail.guava.collection.CollectionTest$Person@49476842}
		System.out.println(map.toString());

		Map<String, Integer> map2 = Maps.<String, Person, Integer>transformEntries(map, (k, v) -> {
			return v.age;
		});
		// {zcy2=31, zcy=11, zcy1=21}
		System.out.println(map2.toString());

		Range<Integer> range = Range.openClosed(11, 21);
		Predicate<Person> predicate = Predicates.compose(range, (Person p) -> {
			return p.age;
		});
		Map<String, Person> newMap = Maps.filterValues(map, predicate);
		// {zcy1=com.angysnail.guava.collection.CollectionTest$Person@27c170f0}
		System.out.println(newMap.toString());
	}

	static class Person {
		private String name;
		private int age;
		private int gender;

		public Person(String name, int age, int gender) {
			super();
			this.name = name;
			this.age = age;
			this.gender = gender;
		}

	}

}
